package jun.empdepmanagersystem02.interseptor.config;


import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.until.JWTuntils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterseptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Result result;
        if ((result = JWTuntils.verify(request.getHeader("token"))).getCode() == 1) {
            System.out.println("登录成功");
            return true;
        }
        System.out.println("登录失败");
//       重点 一定要按照需求文档来开发 返回内容的结构一定要一样 null也不能省略！！
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(gson.toJson(result));
        System.out.println(result);
        return false;
    }
}
